<?
/**
*	@author Ataul Haque
*	@version .03
*	@first_created 1/5/08 
*	@last_modified 1/5/08
*	
*/

$field = array("abc","def","ghi");
$table = "tableName";

function writeFile($FileName,$data){
	$fpt = fopen($FileName,'a');
	fwrite($fpt,$data);
	fwrite($fpt,"\n");
}
/****************************************** addPHP **********************************/
	function addPHP($FileName)
	{
		global $field,$table;
		$b="<?";
		writeFile($FileName,$b);
		
		$b="require('../init.php');";
		writeFile($FileName,$b);
		
		for($i=0;$i<sizeof($field);$i++)
		{
			$b="$";
			$b.=$field[$i];
			$b.=" = addslashes(\$_POST['";
			$b.=$field[$i];
			$b.="']);";
			writeFile($FileName,$b);
		}
		
		$b="			\$sql = <<<SQL";
		writeFile($FileName,$b);	
		$b="";
		writeFile($FileName,$b);	
		$b="INSERT INTO ";
		$b.=$table;
		writeFile($FileName,$b);	
		$b="(";
		for($i=0;$i<sizeof($field);$i++)
		{			
			$b.=$field[$i];
			if(i==sizeof($field)-1)
				$b.=" ";
			else
				$b.=" , ";
		}
		$b.=")";	
		writeFile($FileName,$b);		
		$b="VALUES(";		
		for($i=0;$i<sizeof($field);$i++)
		{
			$b.="'$";
			$b.=$field[$i];
			if(i==sizeof($field)-1)
				$b.="' ";
			else
				$b.="' , ";
		}
		$b.=")";	
		writeFile($FileName,$b);	
		$b="";
		writeFile($FileName,$b);	
		$b="SQL;";
		writeFile($FileName,$b);	
		$b="	mysql_query($sql) or die(mysql_error());";
		writeFile($FileName,$b);			
		$b= " echo \"Successfully added new ";
		$b.=$table;
		$b.="\";";
		writeFile($FileName,$b);		
		$b="?>";
		writeFile($FileName,$b);	
	}
	
/********************************* HTMLForm *************************/
	function HtmlForm($FileName)
	{
		global $field,$table;
		$b="<form method=\"post\" action=\"add_";
		$b.=$table;
		$b.="_submit.php\">";
		writeFile($FileName,$b);
		$b="<table>";
		writeFile($FileName,$b);
		for($i=0;$i<sizeof($field);$i++)
		{
			$b="	<tr>";
			writeFile($FileName,$b);
			$b="		<td>";			
			$b.=$field[$i];
			$b.="</td>";
			writeFile($FileName,$b);
			
			$b="		<td><input type=\"text\" name=\"";			
			$b.=$field[$i];
			$b.="\"></td>";
			writeFile($FileName,$b);
			
			$b="	</tr>";
			writeFile($FileName,$b);
		}
		$b="	<tr>";
		writeFile($FileName,$b);
		$b="		<td>";			
		$b.="&nbsp";
		$b.="</td>";
		writeFile($FileName,$b);
		$b="		<td><input type=\"submit\"\\></td>";			
		writeFile($FileName,$b);
		$b="	</tr>";
		writeFile($FileName,$b);	
		$b="</table>";
		writeFile($FileName,$b);
		$b="</form>";
		writeFile($FileName,$b);
	}
/******************************************** Edit HTML 2 *****************************/
	
	function editHTML2($FileName)
	{				
		global $field,$table;		
		writeFile($FileName,$b);		
		$b="<?";
		writeFile($FileName,$b);
		$b="function getData(\$id){";
		
		writeFile($FileName,$b);
		$b="require('../init.php');";
		writeFile($FileName,$b);
		$b="	\$sql = <<<SQL";
		writeFile($FileName,$b);	
		$b="";
		writeFile($FileName,$b);	
		$b="SELECT * FROM ";
		$b.=$table;
		$b.=" WHERE id=\$id";
		writeFile($FileName,$b);	
		$b="";
		writeFile($FileName,$b);	
		$b="SQL;";
		writeFile($FileName,$b);
		$b="";
		writeFile($FileName,$b);
		$b="	\$result = mysql_query(\$sql) or die(mysql_error());";
		writeFile($FileName,$b);					
		
		$b="";
		writeFile($FileName,$b);
		$b="	echo '<tr><form method=\"post\" action=\"edit_";
		$b.=$table;
		$b.="_submit.php";
		$b.="\">';";
		writeFile($FileName,$b);
		for($i=0;$i<sizeof($field);$i++)
		{
			$b="	$";
			$b.=$field[$i];
			$b.=" = mysql_result(\$result,0,\"";
			$b.=$field[$i];
			$b.="\");";
			writeFile($FileName,$b);						
			$b="		echo \"<td><input type=\\\"text\\\" name=\\\"";			
			$b.=$field[$i];
			$b.="\\\" value=\\\"$";
			$b.=$field[$i];
			$b.="\\\"></td>\";";
			writeFile($FileName,$b);			
		}			
		$b="	echo '<td>";			
		$b.="&nbsp ";
		$b.="</td>";
		writeFile($FileName,$b);
		$b="		<td><input type=\"submit\" value=\"Edit\"></td>";			
		writeFile($FileName,$b);
		$b="	<input type=\"hidden\" name=\"id\" value=\"'.\$id.'\"></td>";					
		writeFile($FileName,$b);
		$b="		<td><a href=\"del_";
		$b.=$table;
		$b.=".php?id='.\$id.'\" >Delete</a></td>";			
		writeFile($FileName,$b);
		$b="	</td>";
		writeFile($FileName,$b);			
		$b="</form></tr>';";
		writeFile($FileName,$b);		
		$b="}";
		writeFile($FileName,$b);
		$b="?>";
		writeFile($FileName,$b);		
	}	
	
/********************************************* Edit submit ***************************/	
	function editPHP_submit($FileName)
	{
		global $field,$table;
		$b="<?";
		writeFile($FileName,$b);
		
		$b="require('../init.php');";
		writeFile($FileName,$b);
		$b="\$id = \$_POST['id'];";
		writeFile($FileName,$b);
		
		for($i=0;$i<sizeof($field);$i++)
		{
			$b="$";
			$b.=$field[$i];
			$b.=" = addslashes(\$_POST['";
			$b.=$field[$i];
			$b.="']);";
			writeFile($FileName,$b);
		}
		
		$b="			\$sql = <<<SQL";
		writeFile($FileName,$b);	
		$b="";
		writeFile($FileName,$b);	
		$b="UPDATE ";
		$b.=$table;
		writeFile($FileName,$b);	
		$b="SET ";
		for($i=0;$i<sizeof($field);$i++)
		{
			$b.=$field[$i];
			$b.=" = '$";
			$b.=$field[$i];
			if(i==$field.length-1)
				$b.="' ";
			else
				$b.="' , ";
		}		
		writeFile($FileName,$b);	
		$b="WHERE id=\$id";
		writeFile($FileName,$b);	
		$b="";
		writeFile($FileName,$b);	
		$b="SQL;";
		writeFile($FileName,$b);	
		$b="	mysql_query(\$sql) or die(mysql_error());";
		writeFile($FileName,$b);			
		$b="echo \"Successfully Edited.\";";
		writeFile($FileName,$b);			
		$b="?>";
		writeFile($FileName,$b);	
	}
/*********************************************** del PHP submit *********************************/
function delPHP_submit($FileName)
	{
		global $field,$table;
		$b="<?";
		writeFile($FileName,$b);
		
		$b="require('../init.php');";
		writeFile($FileName,$b);
		$b="\$id = \$_GET['id'];";
		writeFile($FileName,$b);
		
		$b="			\$sql = <<<SQL";
		writeFile($FileName,$b);	
		$b="";
		writeFile($FileName,$b);	
		$b="DELETE FROM ";
		$b.=$table;
		$b.=" WHERE id=\$id";
		writeFile($FileName,$b);	
		$b="";
		writeFile($FileName,$b);	
		$b="SQL;";
		writeFile($FileName,$b);	
		$b="	mysql_query(\$sql) or die(mysql_error());";
		writeFile($FileName,$b);			
		$b="echo \"Successfully Deleted.\";";
		writeFile($FileName,$b);			
		$b="?>";
		writeFile($FileName,$b);	
		
	}	
	
/**************************************** SQL file ************************************/
	function SQL($FileName){
		global $field,$table;
		$b="CREATE $table ";
		$b.=$table;
		writeFile($FileName,$b);		
		$b=" ( id INT NOT NULL AUTO_INCREMENT PRIMARY KEY ,";
		writeFile($FileName,$b);
		for($i=0;$i<sizeof($field);$i++){
			$b=$field[$i];
			$b.=" VARCHAR( 20 ) NOT NULL";
			if(i!=$field.length-1)
				$b.=",";
			writeFile($FileName,$b);			
		}
		$b=");";
		writeFile($FileName,$b);		
	}
	
/***************************************** get php 2 ***********************************/
function getPHP2($FileName)
	{
		global $field,$table;
		$b="<?";
		writeFile($FileName,$b);
		
		$b="require_once('../init.php');";
		writeFile($FileName,$b);
		$b="require_once('edit_";
		$b.=$table;
		$b.=".php');";
		writeFile($FileName,$b);
		$b="	\$sql = <<<SQL";
		writeFile($FileName,$b);	
		$b="";
		writeFile($FileName,$b);	
		$b="SELECT * FROM ";
		$b.=$table;
		writeFile($FileName,$b);	
		$b="";
		writeFile($FileName,$b);	
		$b="SQL;";
		writeFile($FileName,$b);
		$b="";
		writeFile($FileName,$b);
		$b="	\$result = mysql_query(\$sql) or die(mysql_error());";
		writeFile($FileName,$b);					
		$b="\$num=mysql_num_rows(\$result);";
		writeFile($FileName,$b);
		$b="";
		writeFile($FileName,$b);
		$b="echo '<\table><tr>";		
		writeFile($FileName,$b);	
		for($i=0;$i<sizeof($field);$i++){
			$b="<td>";
			$b.=$field[$i];
			$b.="</td>";			
			writeFile($FileName,$b);		
		}

		$b="</tr>';";
		writeFile($FileName,$b);
		
		
		$b="for(\$i=0;\$i<\$num;\$i++)";
		writeFile($FileName,$b);
		$b="{";
		writeFile($FileName,$b);		
		$b="	\$id=mysql_result(\$result,\$i,'id');";
		writeFile($FileName,$b);
		$b="	getData(\$id);";
		writeFile($FileName,$b);			
		$b="}";
		writeFile($FileName,$b);	
		
		$b="echo '</table>';";
		writeFile($FileName,$b);		
		$b="?>";
		writeFile($FileName,$b);
	}	
	/*********************************** CRUD ************************************/
	function CRUD(){		
		global $field;
		global $table;
		mkdir($table,777);
		$absPath = getcwd();
		$workingDir = $absPath."/".$table."/";
		echo "eable is ".$table;
		
		HtmlForm($workingDir."add_".$table.".php");
		addPHP($workingDir."add_".$table."_submit.php");
		editHTML2($workingDir."edit_".$table.".php");
		editPHP_submit($workingDir."edit_".$table."_submit.php");
		delPHP_submit($workingDir."del_".$table.".php");
		getPHP2($workingDir."get_".$table.".php");
		SQL($workingDir.$table.".sql");
		
	}
	
	CRUD();
	
	?>